package com.baijiayun.liveuibase.widgets.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.baijiayun.liveuibase.R;

/**
 * Created by pengwenjian on 2021/6/21
 * Describe:
 */
public class DotProgressView extends View {
    private int dotRadius = 4;
    private int dotColor = 0;
    private int maxCount = 5;
    private int count = 1;
    private boolean isAdd = true;
    private Paint paint;
    private RectF oval;

    public DotProgressView(Context context) {
        this(context,null);
    }

    public DotProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DotProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DotProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BJYDotProgressView);
            dotRadius = typedArray.getDimensionPixelSize(R.styleable.BJYDotProgressView_bjlive_dot_radius, 4);
            dotColor = typedArray.getColor(R.styleable.BJYDotProgressView_bjlive_dot_color, ContextCompat.getColor(context,R.color.base_icon_color));
            maxCount = typedArray.getInt(R.styleable.BJYDotProgressView_bjlive_dot_count, 5);
            typedArray.recycle();
        }
        if (dotColor == 0) {
            dotColor = ContextCompat.getColor(context, R.color.base_icon_color);
        }
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(dotColor);
        oval = new RectF();
    }

    @SuppressLint("DrawAllocation")
    @Override
    //圆点从1到max再从max到1循环
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        //圆心到圆心的距离
        float space = (width - getPaddingStart() - getPaddingEnd() - 2.0f * dotRadius) / (maxCount - 1);
        //由中心点算出起始点位置
        float x = width / 2f - (maxCount - 1) / 2.0f * space;
        float y = height - dotRadius - 2;
        for (int i = 0; i < count; i++) {
            oval.left = x - dotRadius;
            oval.top = y - dotRadius;
            oval.right = x + dotRadius;
            oval.bottom = y + dotRadius;
            canvas.drawOval(oval, paint);
            x += space;
        }
        if (isAdd) {
            count++;
        } else {
            count--;
        }
        if (count >= maxCount) {
            isAdd = false;
            count = maxCount;
        }
        if (count <= 1) {
            isAdd = true;
            count = 1;
        }
        postInvalidateDelayed(200);
    }
}
